package com.baidu.ota.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.baidu.commonlib.interfaces.IOtaAgent;
import com.baidu.ota.R;
import com.baidu.ota.interfaces.IActivityProxy;
import com.baidu.ota.interfaces.ISystemInfo;
import com.baidu.ota.view.PageViewItem;

import java.util.List;

/**
 * LaunchActivity的代理实现类，类名必须为ActivityImpl
 */
public class ActivityImpl implements IActivityProxy {
    private final Activity host;
    private final ISystemInfo systemInfo;

    private ViewPager viewPager;
    private IOtaAgent otaAgent;

    public ActivityImpl(Activity activity, ISystemInfo info) {
        this.host = activity;
        this.systemInfo = info;
    }

    @Override
    public void onCreate() {
        host.setContentView(R.layout.activity_main);
        viewPager = host.findViewById(R.id.view_pager);
    }

    @Override
    public void onStart() {
        // ignore
    }

    @Override
    public void onRestart() {
        // ignore
    }

    @Override
    public void onStop() {
        // ignore
    }

    @Override
    public void onPause() {
        host.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onResume() {
        host.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onDestroy() {
        // ignore
    }

    @Override
    public void onWindowFocusChanged(boolean focus) {
        navigationBarStatusBar(host, focus);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    }

    @Override
    public void onBind(List<IOtaAgent> agents) {
        otaAgent = agents.get(0);
        initData();
    }

    private void initData() {
        createSubViews();
        viewPager.setCurrentItem(0);
    }

    private void createSubViews() {
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 1;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view.equals(object);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return otaAgent.getProductId();
            }

            public Object instantiateItem(ViewGroup container, int position) {
                View view = LayoutInflater.from(host).inflate(R.layout.pageview_item, container, false);
                container.addView(view);

                PageViewItem item = (PageViewItem) view;
                item.initData(host, systemInfo, otaAgent, position);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
    }

    private static void navigationBarStatusBar(Activity activity, boolean hasFocus) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            activity.getWindow().setNavigationBarColor(Color.TRANSPARENT);
//            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
//        }

        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}
